//
//  PPPreviewPetPhotoViewController.m
//  pet
//
//  Created by 陈无忌 on 12/27/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPPreviewPetPhotoViewController.h"
#import "PPImageDisplayViewController.h"
#import "ImageWaterView.h"
#import "supportingHeadFiles.h"
//#import "PPPetPhoto.h"

@interface PPPreviewPetPhotoViewController ()
@property (weak, nonatomic) IBOutlet ImageWaterView *imageDisplayView;
@property (strong, nonatomic) NSMutableArray *imageArray;

@end

@implementation PPPreviewPetPhotoViewController
-(NSMutableArray *)imageArray
{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [HUD showUIBlockingIndicatorWithText:@"正在加载"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectImage:) name:@"selectImage" object:nil];
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.imageArray removeAllObjects];
        for (PPPetPhoto *petImg in self.pet.petImgList) {
            NSData *data = [NSData dataWithContentsOfURL:[PPImageUrl getImageUrlOriginal:petImg.img]];
            UIImage *img = [UIImage imageWithData:data];
            NSDictionary *imgData = @{
                                      @"thumbURL": [NSString stringWithFormat:@"%@/imgFile?fileName=%@",[PPAPIList getAPIList].rootUrl,petImg.img],
                                      @"width": @(img.size.width),
                                      @"height": @(img.size.height)
                                      };
            ImageInfo *imageInfo = [[ImageInfo alloc]initWithDictionary:imgData];
            [self.imageArray addObject:imageInfo];
        }
        
        [self.imageDisplayView setDataArray:self.imageArray];
        [HUD hideUIBlockingIndicator];
        
    });
    
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)selectImage:(NSNotification *)notification{
    NSDictionary *dic = [notification userInfo];
    NSNumber *selectNum = dic[@"selectIndex"];
    PPImageDisplayViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPImageDisplayViewController"];
    targetVC.pet = self.pet;
    targetVC.index = selectNum.intValue;
    [self.navigationController pushViewController:targetVC animated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
